package com.ht.module.bus.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.abnormal.HtException;
import com.ht.constant.BusConstant;
import com.ht.module.bus.dto.ScreenJsonDto;
import com.ht.module.bus.entity.BusScreen;
import com.ht.module.bus.mapper.BusScreenMapper;
import com.ht.module.bus.service.IBusScreenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.util.Pager;
import com.ht.util.RedisUtil;
import com.ht.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 大屏表 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2021-07-01
 */
@Service
public class BusScreenServiceImpl extends ServiceImpl<BusScreenMapper, BusScreen> implements IBusScreenService {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Pager<BusScreen> findPageByMe(Pager<BusScreen> pager, BusScreen busScreen) {
        QueryWrapper<BusScreen> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(busScreen.getName())){
            qw.like("tab.name",busScreen.getName());
        }
        if(StrUtil.isNotEmpty(busScreen.getIsEncrypt())){
            qw.eq("tab.is_encrypt",busScreen.getIsEncrypt());
        }

        if(StrUtil.isNotEmpty(busScreen.getIsShare())){
            qw.eq("tab.is_share",busScreen.getIsShare());
        }
        qw.eq("tab.create_user", UserUtil.getUserId());
        return baseMapper.findPage(pager,qw);
    }

    @Override
    public Pager<BusScreen> findPageByShare(Pager<BusScreen> pager, BusScreen busScreen) {
        QueryWrapper<BusScreen> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(busScreen.getName())){
            qw.like("tab.name",busScreen.getName());
        }
        if(StrUtil.isNotEmpty(busScreen.getIsEncrypt())){
            qw.eq("tab.is_encrypt",busScreen.getIsEncrypt());
        }
        qw.eq("tab.is_share", BusConstant.YES);
        return baseMapper.findPage(pager,qw);
    }

    @Override
    public Pager<BusScreen> findPage(Pager<BusScreen> pager, BusScreen busScreen) {
        QueryWrapper<BusScreen> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(busScreen.getName())){
            qw.like("tab.name",busScreen.getName());
        }
        if(StrUtil.isNotEmpty(busScreen.getIsEncrypt())){
            qw.eq("tab.is_encrypt",busScreen.getIsEncrypt());
        }
        if(StrUtil.isNotEmpty(busScreen.getIsShare())){
            qw.eq("tab.is_share",busScreen.getIsShare());
        }
        return baseMapper.findPage(pager,qw);
    }

    @Override
    public void saveScreenJson(ScreenJsonDto dto) {
        //查询出当前大屏
        BusScreen busScreen=baseMapper.selectById(dto.getId());
        if(ObjectUtil.isEmpty(busScreen)){
            throw new HtException("大屏不存在！");
        }
        //判断当前用户是否是创建用户
        if(!busScreen.getCreateUser().equals(UserUtil.getUserId())){
            throw new HtException("没有权限操作该大屏");
        }
        //将json数据存在redis里面
        redisUtil.set(BusConstant.SCREEN_JSON+dto.getId(),dto.getJson());
    }

    @Override
    public String getScreenJson(String id) {
        //查询出当前大屏
        BusScreen busScreen=baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(busScreen)){
            throw new HtException("大屏不存在！");
        }
        //判断当前用户是否是创建用户
        if(!busScreen.getCreateUser().equals(UserUtil.getUserId())){
            throw new HtException("没有权限操作该大屏");
        }
        Object json = redisUtil.get(BusConstant.SCREEN_JSON + id);
        if(ObjectUtil.isEmpty(json)){
           return null;
        }
        return json.toString();
    }

    @Override
    /*
     * @param id:大屏id
     * @Author hejialun
     * @Description: TODO(删除大屏)
     * @date 2021/7/8 14:24
     * @returns void
     */
    public void delById(String id) {
        //查询出当前大屏
        BusScreen busScreen=baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(busScreen)){
            throw new HtException("大屏不存在！");
        }
        //判断当前用户是否是创建用户
        if(!busScreen.getCreateUser().equals(UserUtil.getUserId())){
            throw new HtException("没有权限操作该大屏");
        }
        //删除数据库实例表
        baseMapper.deleteById(id);
        //删除redis数据
        redisUtil.del(BusConstant.SCREEN_JSON+id);
    }
}
